<?php
namespace app\app\controller\v1;
use app\app\Appbase;

class Goods extends Appbase 
{
	
	/**
	*获取商品分类
	* /app/v1/goods/getGoodsCate
	* 需要登录 
	* @is_need_login true
	*/
	public function getGoodsCate(){
		try {
			$shop_id = request()->shopId;
			$result =  model('GoodsCate')->getList($shop_id);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	*获取商品列表
	* /app/v1/goods/getList
	* 需要登录 
	* @is_need_login true
	* @method get
	*/
	 function getList($cate_id='',$name=''){
		try {
			$result = model('Goods')->getList($cate_id,$name);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 最近购买商品
	* /app/v1/goods/latelyBuy
	* 需要登录 
	* @is_need_login true
	*@method post 
	*/
	public function latelyBuy(){
		try {
			$result = model('Goods')->latelyBuy(request() ->userId);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	*获取商品明细
	* /app/v1/goods/details
	* 需要登录 
	* @is_need_login false
	* @method get
	*/
	public function details($id){
		if(!$id){
			return $this->ReError('商品id不能为空！');
		}
		try {
			$result = model('Goods')->details($id);
			if(!$result){
			 	return $this->ReError('商品已失效！');
			}
			if($uid = request() ->userId) {
				model('GoodsHistory') ->addHistory($uid,$result['shop_id'],$id);
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	*获取商品评论列表
	* /app/v2/goods/getGoodsReplyList
	* 需要登录 
	* @is_need_login false
	* @method get
	*/
	public function getGoodsReplyList($id,$pageIndex=1,$pageSize=20){
		if(!$id){
			return $this->ReError('商品id不能为空！');
		}
		try {
			$result = model('Goods')->getGoodsReplyList($id,$pageIndex,$pageSize);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 收藏或取消收藏
	* /app/v1/goods/collect
	* 需要登录 
	* @is_need_login true
	*@method post 
	*/
	public function collect($goods_id){
		$uid = request() ->userId;
		$param['uid'] =$uid;
		$param['goods_id'] = $goods_id;
		$param['type'] = 'collect';
		$param['shop_id'] = model('Goods')->where('id',$goods_id)->value('shop_id');
		if(!$param['shop_id'] && $param['shop_id'] != 0){
			return $this->ReError("找不到该商品！");
		}
		
		try {
			$db = model('UserStars');
			$is_collect = $db->where('uid',$uid)->where('type','collect')
				->where('goods_id',$goods_id)
				->find();
			
			if( !$is_collect  ){
				$result = $db->add($param);
			}else{
				$result = $db->delCollect($uid,$goods_id);
			}
			
			if($result === false){
				return $this->ReError( $db->errorInfo );
			}
			return $this->ReSucess(null,  $is_collect ? '已取消收藏！' : '收藏成功！');	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 点赞或取消点赞
	* /app/v1/goods/like
	* 需要登录 
	* @is_need_login true
	*@method post 
	*/
	public function like($goods_id){
		$uid = request() ->userId;
		$param['uid'] =$uid;
		$param['goods_id'] = $goods_id;
		$param['type'] = 'like';
		$param['shop_id'] = model('Goods')->where('id',$goods_id)->value('shop_id');
		if(!$param['shop_id']){
			return $this->ReError("找不到该商品！");
		}
		
		try {
			$db = model('UserStars');
			$is_collect = $db->where('uid',$uid)->where('type','like')
				->where('goods_id',$goods_id)
				->find();
				
			if( !$is_collect  ){
				$result = $db->add($param);
			}else{
				$result = $db->delLike($uid,$goods_id);
			}
			
			if($result === false){
				return $this->ReError( $db->errorInfo );
			}
			return $this->ReSucess(null,  $is_collect ? '已取消点赞！' : '点赞成功！');	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
}



